package vip.zhaotao.controller;

import freemarker.template.TemplateException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import vip.zhaotao.base.BaseController;
import vip.zhaotao.utils.ExcelUtils;
import vip.zhaotao.utils.FileUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Excel Controller
 */
@Controller
public class ExcelController extends BaseController {

    /**
     * 生成docx文件
     *
     * @param response
     */
    @RequestMapping(value = "/xlsx", method = RequestMethod.GET)
    public void genDocx(HttpServletResponse response) throws IOException, TemplateException {
        // 响应文件名称（即最终下载的文件名称）
        String responseFileName = "test.xlsx";
        this.setXlsxHeader(response, responseFileName, FileUtils.utf8().name());

        Map<String, Object> dataModel = new HashMap<>();
        dataModel.put("name", "中华人民共和国");
        ExcelUtils.generateXlsxFile(response.getOutputStream(), dataModel);
    }
}
